package experiment4.nqueen;

import java.util.Scanner;

public class NQueen {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        NQueen queen = new NQueen();
        int n;
        while ((n = scanner.nextInt()) != -1) {
            queen.solution(n);
        }

    }
    //k = (y1 - y2)/(x1 - x2) in (-1,0,1,INF)则 (x1,y1)和(x2,y2)在同一直线上
    //当k = 1时，y1 = x1 + n,y2 = x2 + n,两式相减y1-y2 == x1-x2
    //当k = -1时，y1 = -x1 + n,y2 = -x2 + n,两式相减y1-y2 = x2 - x1
    //当k = 0时，y1 = y2
    //当k = INF时，x1 = x2

    int f[];
    int sum;

    boolean check(int x) {
        for (int i = 0; i < x; i++) {
            if (Math.abs(x - i) == Math.abs(f[x] - f[i]) || f[x] == f[i]) {
                return false;
            }
        }
        return true;
    }

    public int solution(int n) {
        sum = 0;
        f = new int[n];
        addQueen(0);
        return 0;
    }

    public void addQueen(int x) {
        if (x > f.length - 1) {
            printResult();
            return;
        }
        for (int j = 0; j < f.length; j++) {
            f[x] = j;
            if (check(x)) {
                addQueen(x + 1);
            }
        }
    }

    public void printResult() {
        sum++;
        System.out.println("第" + sum + "种");
        for (int i = 0; i < f.length; i++) {
            for (int j = 0; j < f.length; j++) {
                if (f[j] == i) {
                    System.out.print("# ");
                } else {
                    System.out.print("0 ");
                }

            }
            System.out.println();
        }
    }
}

